clear;
%===============================================
data = load('ex1data2.txt');
% X = data(:, 1);
% X = [X, data(:, 2)];
X = data(:, 1:2);
y = data(:, 3);
m = length(y);

fprintf('First 10 examples from the dataset: \n');
fprintf(' X = [%.0f %.0f], y = %.0f \n', [X(1:10, :), y(1:10, :)]');

%===============================================

fprintf('Normalizing Features ...\n');
[X_norm, mu, sigma] = featureNormalize(X);
X_norm = [ones(m, 1), X_norm];

%===============================================

fprintf('Running gradient descent ...\n');
alpha1 = 0.01;
alpha2 = 0.03;
alpha3 = 0.1;
alpha4 = 0.3;
% alpha5 = 1.4;


num_iters = 8500;
theta = zeros(3, 1);
[theta1, J_history1, theta_history1] = gradientDescent(X_norm, y, theta, alpha1, num_iters);
[theta2, J_history2, theta_history2] = gradientDescent(X_norm, y, theta, alpha2, num_iters);
[theta3, J_history3, theta_history3] = gradientDescent(X_norm, y, theta, alpha3, num_iters);
[theta4, J_history4, theta_history4] = gradientDescent(X_norm, y, theta, alpha4, num_iters);
% [theta5, J_history5, theta_history5] = gradientDescent(X_norm, y, theta, alpha5, num_iters);


figure;
plot(1:numel(J_history1), J_history1, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
hold on;
plot(1:numel(J_history2), J_history2, '-r', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');

plot(1:numel(J_history3), J_history3, '-k', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');

plot(1:numel(J_history4), J_history4, '-g', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');

% plot(1:numel(J_history5), J_history5, '-y', 'LineWidth', 2);
% xlabel('Number of iterations');
% ylabel('Cost J');

fprintf('Theta computed from gradient descent: \n');
fprintf(' %f %f %f \n', [theta1, theta2, theta3, theta4]);

price = [1, (([1650, 3] - mu) ./ sigma)] * theta1;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
    '(using gradient descent):\n $%f\n'], price);

X = [ones(size(X, 1), 1), X];
theta5 = pinv(X' * X) * X' * y;
fprintf('Theta computed from the normal equations: \n');
fprintf(' %f \n', theta5);
fprintf('\n');
price = [1 1650 3] * theta5;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
    '(using normal equations):\n $%f\n'], price);
